package com.tanhua.server.service;

import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.PublishApi;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class MovementMQService {

    @Autowired
    private RocketMQTemplate mqTemplate;
    @Autowired
    private PublishApi publishApi;

    //发布动态
    public void sendMsgWithSave(String publishId) {
        sendMessage(publishId, 1);
    }

    public void sendMsgWithBrowse(String publishId) {
        sendMessage(publishId, 2);
    }

    //点赞动态
    public void sendMsgWithLike(String publishId) {
        sendMessage(publishId, 3);
    }

    //喜欢动态
    public void sendMsgWithLove(String publishId) {
        sendMessage(publishId, 4);
    }

    //评论动态
    public void sendMsgWithComment(String publishId) {
        sendMessage(publishId, 5);
    }

    //取消点赞
    public void sendMsgWithDislike(String publishId) {
        sendMessage(publishId, 6);
    }

    //取消喜欢动态
    public void sendMsgWithUnlove(String publishId) {
        sendMessage(publishId, 7);
    }



    private void sendMessage(String publishId, Integer type) {
        Long userId = UserHolder.getUser().getId();
        Publish publish = publishApi.findById(new ObjectId(publishId));
        Map<String, Object> message = new HashMap<>();

        message.put("userId", userId);
        message.put("publishId", publish.getPid());
        message.put("type", type);

        mqTemplate.convertAndSend("tanhua-quanzi", message);
        log.info("【MQ消息】用于大数据推荐动态, topic:{}, message:{}","tanhua-quanzi", message);
    }
}
